<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_SalesInvoiceController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        parent::loadTemplate('alpona');
        $this->view->headTitle = "create sales invoice";
	}
	
	public function indexAction(){
		$scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
		$model = new Admin_Model_Customer();
        $data = $model->getDatas();
        
        $this->view->action = 'create';
        $this->view->data = $data;
	}
    
    public function createAction(){
        parent::addScripts(array('jquery-price-format.min.js', 'autoNumeric.js', 'jquery-ui-1.10.3.custom.min.js'));
        parent::addStyleSheet(array('jquery-ui-1.10.3.custom.css'));
    	$request = $this->_request;
        
        if($request->isPost()){
            $customer_id = $request->getParam('customer_id');
            
            $model = new Admin_Model_ProductPrice();
            $products = $model->getDatas($customer_id);
            
            $customer_model = new Admin_Model_Customer();
            $customer = $customer_model->getData($customer_id);
            
            $model_sale_invoice = new Admin_Model_SalesInvoice();
            
            $model_liabilities = new Admin_Model_Liabilities();
            $dept = $model_liabilities->getDept($customer_id);
            $payment = $model_liabilities->getPayment($customer_id);
            $order = $model_liabilities->getOrders($customer_id);
            $refund = $model_liabilities->getRefund($customer_id);
            $customer_total = $model_liabilities->getCustomerTotal($customer_id);
            
            $this->view->customer_total = $customer_total;
            $this->view->refund = $refund;
            $this->view->order = $order;
            $this->view->dept = $dept;
            $this->view->payment = $payment;
            $this->view->products = $products;
            $this->view->customer_id = $customer_id;
            $this->view->customer = $customer;
            
            $save = $request->getParam('save', false);
            $has_product = $request->getParam('product', false);
            if($save && $has_product){
                $validator = new Admin_Form_ValidateSalesInvoice($request->getParams());
                
                $data = $validator->getData();
                
                $orderID = $model_sale_invoice->saveData($data);
                
                $refund = array();
                $refund['customer_id'] = $data['customer_id'];
                $refund['total'] = $data['refund'];
                $refund['date'] = $data['date'];
                $refund['order_id'] = $orderID;
                
                $refund_model = new Admin_Model_Refund();
                $refund_model->saveData($refund);
                
                $this->view->orderID = $orderID;
                //$this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'sales-invoice', 'action' => 'view', 'order_id' => $orderID), null, true);
            }
        }
    }
    
    public function editAction(){
        parent::addScripts(array('jquery-price-format.min.js', 'autoNumeric.js'));
    	$request = $this->_request;
        
        $customer_id = $request->getParam('customer_id');
        $order_id = $request->getParam('id');
        
        $model = new Admin_Model_ProductPrice();
        $products = $model->getDatas($customer_id);
        
        $customer_model = new Admin_Model_Customer();
        $customer = $customer_model->getData($customer_id);
        
        $model_sale_invoice = new Admin_Model_SalesInvoice();
        
        $model_liabilities = new Admin_Model_Liabilities();
        $old_order = $model_sale_invoice->getData($order_id);
        $dept = $model_liabilities->getDept($customer_id);
        $payment = $model_liabilities->getPayment($customer_id);
        $order = $model_liabilities->getOrders($customer_id, $old_order['order']['date']);
        $refund = $model_liabilities->getRefund($customer_id, $old_order['order']['date']);
        $customer_total = $model_liabilities->getCustomerTotal($customer_id);
        
        $refund_model = new Admin_Model_Refund();
        $old_refund = $refund_model->getDataByOrder($order_id);
        
        if($old_refund == null){
            $old_refund = 0;
        }else{
            $old_refund = $old_refund->total;
        }
        
        $this->view->old_order = $old_order;
        $this->view->old_refund = $old_refund;
        $this->view->customer_total = $customer_total;
        $this->view->refund = $refund;
        $this->view->order = $order;
        $this->view->dept = $dept;
        $this->view->payment = $payment;
        $this->view->products = $products;
        $this->view->customer_id = $customer_id;
        $this->view->customer = $customer;
        
        $save = $request->getParam('save', false);
        
        if($save){
            $validator = new Admin_Form_ValidateSalesInvoice($request->getParams(), true);
            $data = $validator->getData();
            
            $refund = array();
            $refund['customer_id'] = $data['customer_id'];
            $refund['total'] = $data['refund'];
            $refund['date'] = $old_order['order']['date'];
            $refund['order_id'] = $data['order_id'];
            
            $remaining = $data['remaining'] - $old_order['order']['remaining'];
            
            $model_sale_invoice->updateOrders($remaining, $data['order_id'], $order->customer_id);
            
            $orderID = $model_sale_invoice->saveData($data);
            
            $refund_model->saveData($refund);
            
            $this->view->orderID = $orderID;
            //$this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'sales-invoice', 'action' => 'manage', 'order_id' => $orderID), null, true);
        }
    }
    
    public function viewAction(){
    	$request = $this->_request;
        $orderID = $request->getParam('order_id');
        
        $model = new Admin_Model_SalesInvoice();
        
        $rs = $model->getData($orderID);
        
        $this->view->order = $rs['order'];
        $this->view->products = $rs['products'];
        
        $this->_helper->layout->disableLayout();
    }
    
    public function manageAction(){
        $this->view->headTitle = "manage sales invoice";
    	$scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js');
		parent::addScripts($scripts);
		$model = new Admin_Model_SalesInvoice();
        $data = $model->getDatas();
        
        $this->view->data = $data;
    }
    
    public function removeAction(){
        $request = $this->_request;
        
        if($request->isPost()){
            $model = new Admin_Model_SalesInvoice();
            $rs = $model->deleteData($request->getParam('order_id'));
        }
        
        $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'sales-invoice', 'action' => 'manage'), null, true);
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function deptAction(){
        $this->view->headTitle = "create dept";
        $scripts = array('jquery.dataTables.js', 'dataTables.bootstrap.js', 'TableTools.js', 'autoNumeric.js');
		parent::addScripts($scripts);
		$request = $this->_request;
        
        $model = new Admin_Model_Customer();
        $data = $model->getDatas();
        
        $this->view->action = 'dept';
        $this->view->data = $data;
        $this->_helper->viewRenderer('index');
        
        if($request->isPost()){
            $customer_id = $request->getParam('customer_id');
            
            $customer_model = new Admin_Model_Customer();
            $customer = $customer_model->getData($customer_id);
            
            $this->view->customer_id = $customer_id;
            $this->view->customer = $customer;
            
            $save = $request->getParam('save', false);
            
            if($save){
                $validator = new Admin_Form_ValidateDept($request->getParams());
                
                $model_dept = new Admin_Model_Dept();
                $model_dept->saveData($validator->getData());
                
                $this->_helper->_redirector->gotoRoute(array('module' => 'admin', 'controller' => 'sales-invoice', 'action' => 'dept'), null, true);
            }
            
            $this->_helper->viewRenderer('dept');
        }
    }
}